/*
 * @Description:
 * @Author: your name
 * @version:
 * @Date: 2024-07-26 15:15:37
 * @LastEditors: your name
 * @LastEditTime: 2024-07-26 15:15:40
 */
/*
 * @Description:
 * @Author: your name
 * @version:
 * @Date: 2024-05-09 13:30:09
 * @LastEditors: your name
 * @LastEditTime: 2024-05-14 09:28:20
 */
import api from './request'
import { lineColors } from '@/store/staticData'
import { CoordTransform } from '@/cesiumTools/mapPlugin.js'

export const getLine = async (params) => {
  try {
    const { data, code } = await api.get(`/getLine`, { params })
    if (code === 200) {
      const result = data.map((item, index) => {
        const { xs, ys, stationsList } = item
        const colorIndex = index % lineColors.length
        const color = lineColors[colorIndex]
        item.color = color
        item.checked = true
        
        // 整理坐标
        const xpos = xs.split(',').map((item) => parseFloat(item))
        const ypos = ys.split(',').map((item) => parseFloat(item))
        let positions = []
        if (xpos.length === ypos.length) {
          xpos.forEach((item, index) => {
            const [lng, lat] = CoordTransform.GCJ02ToWGS84(item, ypos[index])
            positions.push({ lng, lat })
          })
        }
        item.paths = positions

        // 处理站点数据
        if (stationsList?.length) {
          item.stationsList = stationsList.map((station) => {
            const { xy_coords, ...rest } = station
            const [lng, lat] = xy_coords.split(';').map((item) => Number(item))
            const [lngWgs84, latWgs84] = CoordTransform.GCJ02ToWGS84(lng, lat)
            return { position: { lng: lngWgs84, lat: latWgs84 }, ...rest }
          })
        }

        return item
      })
      return result
    }
    return []
  } catch (error) {
    console.error('Failed to get line data:', error)
    throw error
  }
}

export const getLinePlan = (params) => api.get(`/getLinePlan`, { params })
export const getStationInfo = (params) => api.get(`/getStationInfo`, { params })

// 天气API - 建议将key移到环境变量
export const getWeather = async () => {
  const params = {
    key: import.meta.env.VITE_WEATHER_API_KEY || '934bdde4c3d4738b0abd16bdffd0b8f7',
    city: '420100',
  }

  try {
    const data = await api.get(
      'https://restapi.amap.com/v3/weather/weatherInfo',
      { params }
    )
    return data
  } catch (error) {
    console.error('Failed to get weather data:', error)
    throw error
  }
}
